package com.bqtt.tumor.demo.business;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bqtt.tumor.demo.common.MyBeanFactory;
import com.bqtt.tumor.demo.data_access.LabSheetDAO;
import com.bqtt.tumor.demo.entity.DiagnosisMethod;
import com.bqtt.tumor.demo.entity.Feature;
import com.bqtt.tumor.demo.entity.LabSheet;
import com.bqtt.tumor.demo.presentation.model.lab_sheet.LabSheetMVC;
import com.bqtt.tumor.demo.presentation.model.lab_sheet.SelectOption;

public class LabSheetService {

	
	private static LabSheetDAO labSheetDAO = (LabSheetDAO) MyBeanFactory.getBean("labSheetDAO");
	private static Logger log = LoggerFactory.getLogger(LabSheetService.class);
	
	public static List<LabSheet> searchByCondition(LabSheet condition,int pageSize,int pageNo)
	{
		
		List<LabSheet> result = labSheetDAO.fuzzyFindByProperties(condition.getSQLValidAttributes(),pageSize,pageNo);
		for(LabSheet item:result)
		{
			item.fillStringField();
		}
		return result;
	}
	
	
	public static List<LabSheet> getList()
	{
		return labSheetDAO.findAll();
	}
	
	/**
	 * @return
	 */
	public static LabSheetMVC getSheetShowHtmlList()
	{
		
		LabSheetMVC res = new LabSheetMVC();
		
		
		List<LabSheet> sheetList = getList();
		
		String onChangeJS = "function fieldInputChange_result_value_id(){";
		onChangeJS += "var hiddenControl = document.getElementById('result_value_id');var result = '';";
		onChangeJS += "for(var i=0;i<fields_number;i++){";
		onChangeJS += "var fieldinput = document.getElementById('result_value_id_field_'+i);";
		onChangeJS += "result=result +fieldinput.value+'|';} ";
		onChangeJS += "hiddenControl.value = result;}";
		
		List<SelectOption> selectList = new ArrayList<SelectOption>();
	
	    String divArray = "[";
		
		for(int i =0 ;i<sheetList.size();i++)
		{
			LabSheet item = sheetList.get(i);
			
			SelectOption selectOption = new SelectOption();
			selectOption.setValue(item.getName());
			
			
			String [] fields =null;
			String [] units =null;
			
			if(item.getFields()!=null && !item.getFields().isEmpty())
			{
				fields = item.getFields().split("\\|");
			}
			
			if(item.getUnit()!=null && !item.getUnit().isEmpty())
			{
				units = item.getUnit().split("\\|");
			}
			
			String html ="";
			if(fields!=null)
			{
				html = "";
				
				for(int j = 0; j < fields.length;j++)
				{
					html = html + "<font>"+fields[j]+"</font>: <input type=\\\"text\\\" onchange=\\\"fieldInputChange('result_value_id',"+fields.length+")\\\" id=\\\"result_value_id_field_"+j+"\\\" class=\\\"fieldinput\\\" />  <font>"+units[j]+"</font><br>";
				}
				
				selectOption.setField_count(fields.length);
				
				//html = html.replace("/", "&frasl;");
				
				divArray = divArray+"\""+html+"\",";
				
			}
		
			selectList.add(selectOption);
		 }
		
		
		divArray = divArray.substring(0,divArray.length()-1)+"]";
		res.setTableSelectOptionList(selectList);
		res.setSheet_div_array_string(divArray);
		
		
		return res;
	}
	
	
	public static LabSheet getSheetByName(String name)
	{
		List<LabSheet>  temp =  labSheetDAO.findByProperty("name", name);
		if(temp!=null && temp.size()>0)
		{
			return temp.get(0);
		}
		return null;
	}
	
	public static List<LabSheet> searchByCondition(LabSheet condition,int pageSize,int pageNo,long id)
	{
		
		List<LabSheet> result = labSheetDAO.fuzzyFindByProperties(condition.getSQLValidAttributes(),pageSize,pageNo);
		
		for(LabSheet item:result)
		{
			if(item.getSheet_def_Id()==id)
			{
				item.setDisplayName("inline-block");
			}
			item.fillStringField();
		}
		return result;
	}
	
	public static boolean add(LabSheet obj)
	{
		try
		{
			obj.fillDBfields();
			labSheetDAO.save(obj);
			return true;
		}
		catch(Exception e)
		{
			log.error(e.getMessage());
	     	return false;	
		}
		
	}
	
	public static boolean update(LabSheet obj)
	{
		try
		{
			if(obj.getSheet_def_Id()>0)
			{
				obj.fillDBfields();
				labSheetDAO.update(obj);
				return true;
			}
			else
				
			return false;
		}
		catch(Exception e)
		{
			log.error(e.getMessage());
	     	return false;	
		}
		
	}
}
